//
//  AccountViewModel.swift
//  weibo
//
//  Created by apple on 16/2/20.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import AFNetworking

class AccountViewModel: NSObject {

    //单例
    static let sharedAccountViewModel: AccountViewModel = AccountViewModel()
    //定义用户账户模型属性
    var userAccount: UserAccount?
    //构造函数
    private override init() {
        super.init()
        userAccount = UserAccount.loadUserAccount()
    }
    //用户是否登录
    var userLogin: Bool {
        return userAccount?.access_token != nil
    }
    
    //计算当前登录用户的token
    var access_token: String? {
        return userAccount?.access_token
    }
    //用户的名称是只读属性
    var userName: String? {
        return userAccount?.name
    }
    
    //用户头像的url
    var headImageURL: NSURL? {
        
        if let urlString = userAccount?.avatar_large {
            return NSURL(string: urlString)
        }
        return nil
    }
//加载用户token
    func loadAccessToken(code: String,finished: (isSuccess: Bool) ->()){
    
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id":client_id,"client_secret": client_secret ,"grant_type":"authorization_code","code":code, "redirect_uri":redirect_uri]
        
        let AFN = AFHTTPSessionManager()
        //设置支持的格式
        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
    
        AFN.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            //尝试转换为字典类型数据
            guard let dict = result as? [String : AnyObject] else {
            
                finished(isSuccess: false)
                return
            }
            let user = UserAccount(dict: dict)
            
            //获取用户信息  发起网络请求
            self.loadUserInfo(user, finished: finished)
            
            }) { (_, error) -> Void in
                
                finished(isSuccess: false)
        }
    }
    //加载用户信息
    private func loadUserInfo(userAccount:UserAccount, finished: (isSuccess: Bool) -> ()) {
    //用户信息接口 
        let urlString = "https://api.weibo.com/2/users/show.json"
        let parameters = ["access_token": userAccount.access_token!, "uid": userAccount.uid!]
        let AFN = AFHTTPSessionManager()
        
        AFN.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            guard let dict = result as? [String : AnyObject] else {
                finished(isSuccess: false)
                return
            
            }
            
            let name = dict["name"] as! String
            let avatar_large = dict["avatar_large"] as! String
            userAccount.name = name
            userAccount.avatar_large = avatar_large
            //归档
            userAccount.saveAccount()
            self.userAccount = userAccount
            
            finished(isSuccess: true)
            }) { (_, error) -> Void in
                finished(isSuccess: false)
        }
        
        
    
    }
    
    
}
